﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinkViewer
{
    public class DirectoryLink : ILink
    {
        private const int SuspendedLinksPageID = 20090;


        public jcc.com.dailyez.Link Link {get; set;}
        public int Page_ID
        {
            get {
                return Link != null ? Link.PageID : 0;
            }
            set
            {
                if (Link != null)
                    Link.PageID = value;
                else
                    throw new Exception("null Link inside LinkObject");
            }
        }
        public void Delete()
        {
            if (Elevated || Suspended)
            {
                try { jcc.StandardWebService.WebService.DeleteBadLink(jcc.StandardWebService.GetKey(), jcc.StandardWebService.WebService.GetBadLink(jcc.StandardWebService.GetKey(), ID).ID); }
                catch { }
            }
            jcc.StandardWebService.WebService.DeleteLink(jcc.StandardWebService.GetKey(), Link.ID);
        }
        public bool Elevated
        {
            get
            {

                //get the link if it exists in the bad links table
                
                jcc.com.dailyez.Bad_Links link = jcc.StandardWebService.WebService.GetBadLink(jcc.StandardWebService.GetKey(), ID);

                //if the link exists, return elevation status, otherwise return false if it doens't exist
                if (link != null)
                    return link.Is_Elevated;
                return false;
            }
            set
            {
                if (value)
                    jcc.StandardWebService.WebService.ElevateLink(ID);
                else
                    jcc.StandardWebService.WebService.UnElevateLink(ID);
            }
        }

        public bool SuspendedProperty { get; set; }
        public bool ElevatedProperty { get; set; }
        public bool Suspended
        {
            get
            {
                
                //see if there's an entry in the bad links table
                var link = jcc.StandardWebService.WebService.GetBadLink(jcc.StandardWebService.GetKey(), ID);

                //if there is an entry, check if it's suspended already, if there isn't an entry it cannot be suspended, so return false
                if (link != null)
                    return link.Is_Suspended;
                else
                    return false;
            }
            set
            {
                if (value == true)
                    jcc.StandardWebService.WebService.SuspendLink(jcc.StandardWebService.GetKey(), ID);
                else
                    jcc.StandardWebService.WebService.UnSuspendLink(jcc.StandardWebService.GetKey(), ID);
            }
        } // Suspended Property

        public string Title
        {
            get
            {
                if (Link != null)
                    return Link.Title;
                else
                    return "";
            }
            set
            {
                if (Link != null)
                    Link.Title = value;
                else
                    throw new Exception("null link exception");
            }
        }
        public string URL
        {
            get
            {
                if (Link != null)
                    return Link.URL;
                else
                    return "";
            }
            set
            {
                if (Link != null)
                    Link.URL = value;
                else
                    throw new Exception("null link exception");
            }
        }

        public int ID
        {
            get
            {
                if (Link != null)
                    return Link.ID;
                else
                    return 0;
            }
            set
            {
                if (Link != null)
                    Link.ID = value;
                else
                    throw new Exception("null link exception");
            }
        }
        public DirectoryLink()
        {

        }

    }
}
